package com.shunbo.convenient.controller;

import com.shunbo.convenient.service.AddressService;
import com.shunbo.convenient.service.AreaService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.utils.MapUtil;
import com.shunbo.convenient.vo.Address;
import com.shunbo.convenient.vo.Area;
import com.shunbo.convenient.vo.Dw;
import com.shunbo.convenient.vo.MapValueComparators;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author weizhuang
 * 地址
 */
@RestController
@RequestMapping("/address")
public class AddressController {


    @Resource
    private AddressService addressService;
    @Resource
    private AreaService areaService;


    /**
     * 收货地址列表
     *
     * @param address 收货地址
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getAddressList(@RequestBody Address address) {
        try {
            List<Address> all = addressService.findAll(address);
            return ConvenientResult.build(200, "成功", all);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加收货地址
     *
     * @param address 收货地址
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveAddress(@RequestBody Address address) {
        try {
            Integer save = addressService.save(address);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新收货地址
     *
     * @param address 收货地址
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateAddress(@RequestBody Address address) {
        try {
            if(!StringUtils.isBlank(address.getStatus()) && "0".equals(address.getStatus())){
                Address address1 = new Address();
                address1.setUserId(address.getUserId());
                address1.setStatus("1");
                addressService.update(address1);
            }
            Integer save = addressService.update(address);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除收货地址
     *
     * @param address 收货地址
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteAddress(@RequestBody Address address) {
        try {
            Integer save = addressService.delete(address.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查收货地址
     *
     * @param address 收货地址
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getAddressById(@RequestBody Address address) {
        try {
            Address st = addressService.findById(address.getId() + "");
            return ConvenientResult.build(200, "成功", st);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


    /**
     * 区下小区列表
     *
     * @return Result结果集
     */
    @RequestMapping("/listAddress")
    public ConvenientResult getAddressList(@RequestBody Dw dw) {
        try {

            Map<Integer, Double> map = new HashMap();
            List<Area> all = areaService.findAll(null);
            List<Area> areaList = new ArrayList<>();
            for (int i = 0; i < all.size(); i++) {
                double distance = MapUtil.Distance(dw.getAccuracy(), dw.getLatitude(), Double.parseDouble(all.get(i).getLng()), Double.parseDouble(all.get(i).getLat()));
                map.put(i, distance);
            }
            List<Map.Entry<Integer, Double>> entryList = new ArrayList<>(
                    map.entrySet());
            Collections.sort(entryList, new MapValueComparators());

            for (Integer key : map.keySet()) {//keySet获取map集合key的集合  然后在遍历key即可
                if (areaList.size() < 5) {
                    areaList.add(all.get(key));
                }
            }
            return ConvenientResult.build(200, "成功", areaList);

        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

}
